package com.taillive.commons;

import javax.servlet.http.Cookie;

public class CookieUtils {

	public static Cookie getCookie(Cookie[] cookies, String name) {
		if (name == null || name.equals(""))
			return null;

		Cookie cookie = null;
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				if (cookies[i].getName().equals(name)) {
					cookie = cookies[i];
					break;
				}
			}
		}
		return cookie;
	}
	
	
	public static String getCookieValue(Cookie[] cookies,String name) {
		Cookie cookie = getCookie(cookies,name);
		return getCookieValue(cookie);
	}	
	
	public static String getCookieValue(Cookie cookie) {
		if (cookie == null){
			return "";
		}
		else{
			return clearString(cookie.getValue());
		}
	}
	
	
	
	public static String clearString(String str) {
		if (str == null || str.equals(""))
			return "";

		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if (c != '\\' && c != '"') {
				buf.append(c);
			}
		}

		return buf.toString();
	}		
	
}

